//
//  ZYXVideoPlayVC.m
//  RuiBeiKang
//
//  Created by iOS开发1 on 2019/1/8.
//  Copyright © 2019年 JW. All rights reserved.
//

#import "ZYXVideoPlayVC.h"
#import "SelVideoPlayer.h"
#import "SelPlayerConfiguration.h"
#import "AppDelegate.h"
#import <Masonry/Masonry.h>


#import "ZYXClassNameVC.h"
#import "ZYXClassAlterView.h"
#import "ZYXBuyVC.h"
#import "ZYXCourseDetVC.h"
#import "ZYXDownLoadVC.h"
@interface ZYXVideoPlayVC ()<UIScrollViewDelegate,ZYXClassNameVCDelegate>
@property (nonatomic, strong) SelVideoPlayer *player;
@property (nonatomic, strong) SelPlayerConfiguration *configuration;

@property (strong,nonatomic) UIImageView *backImgview;

@property (weak, nonatomic) IBOutlet UIView *videoView;
@property (weak, nonatomic) IBOutlet UIView *lineView;

@property (weak, nonatomic) IBOutlet UIScrollView *scrollerView;
@property (strong,nonatomic) NSString *typeStr;
@property (strong,nonatomic) ZYXClassAlterView *alterView;
@property (assign,nonatomic) BOOL isCanDown;

@property (strong,nonatomic) UIButton *freeBtn;

@property (strong,nonatomic) ZYXClassNameVC *vcOne;

@property (strong,nonatomic)ZYXCourseDetVC *vcTwo;
@end

@implementation ZYXVideoPlayVC


- (void)viewDidLoad {
    [super viewDidLoad];
    [self loadData];
    self.view.backgroundColor = [UIColor whiteColor];
    
    if ([[UIDevice currentDevice].systemVersion floatValue] > 7.0) {
        self.edgesForExtendedLayout = UIRectEdgeNone;
    }
    //    http://vhost1015.zihaiweb.cn//Public//upload//music//2018-12-15//5c14a041194df.mp3
    //    http://rajy.oss-cn-hangzhou.aliyuncs.com/vke/%E4%B8%83%E5%B9%B4%E7%BA%A7%E4%B8%8B%E5%86%8C%E5%86%8C%E5%A4%9A%E5%AA%92%E4%BD%93DVD%E5%86%85%E5%AE%B9/UNIT5%20Our%20School%20Life%20TOPIC%202%20A%20few%20students%20are%20running%20around%20the%20playground%20Section%20D%20.mp4
    
    self.typeStr = @"mp3";
    self.configuration = [[SelPlayerConfiguration alloc]init];
    self.configuration.shouldAutoPlay = YES;     //自动播放
    self.configuration.supportedDoubleTap = YES;     //支持双击播放暂停
    self.configuration.shouldAutorotate = YES;   //自动旋转
    self.configuration.repeatPlay = NO;     //重复播放
    self.configuration.statusBarHideState = SelStatusBarHideStateNever;     //设置状态栏隐藏
//    self.configuration.sourceUrl = [NSURL URLWithString:@"http://rajy.oss-cn-hangzhou.aliyuncs.com/vke/%E4%B8%83%E5%B9%B4%E7%BA%A7%E4%B8%8B%E5%86%8C%E5%86%8C%E5%A4%9A%E5%AA%92%E4%BD%93DVD%E5%86%85%E5%AE%B9/UNIT5%20Our%20School%20Life%20TOPIC%202%20A%20few%20students%20are%20running%20around%20the%20playground%20Section%20D%20.mp4"];     //设置播放数据源
    self.configuration.videoGravity = SelVideoGravityResize;   //拉伸方式
    self.backImgview = [[UIImageView alloc]initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH,250)];
    self.backImgview.image = [UIImage imageNamed:@"mp3fengmian"];
    self.backImgview.userInteractionEnabled = YES;
    if ([self.typeStr isEqualToString:@"mp4"]) {
        [self.videoView addSubview:_player];
       
    }else{
        [self.videoView addSubview:self.backImgview];
        [self.backImgview addSubview:_player];
    }

  
    
    
    self.vcOne = [[ZYXClassNameVC alloc]init];
    self.vcOne.zid = self.zid;
    self.vcOne.view.frame = CGRectMake(0, 0, SCREEN_WIDTH,self.scrollerView.height);
    self.vcOne.delegate = self;
    [self.vcOne.buyAtOnce addTarget:self action:@selector(clickBuy) forControlEvents:UIControlEventTouchUpInside];
    [self.vcOne.downLoadBtn addTarget:self action:@selector(clicDown) forControlEvents:UIControlEventTouchUpInside];
    [self addChildViewController:self.vcOne];
    [self.scrollerView addSubview:self.vcOne.view];
    
    
    self.vcTwo = [[ZYXCourseDetVC alloc]init];
    self.vcTwo.zid = self.zid;
    self.vcTwo.view.frame = CGRectMake(SCREEN_WIDTH, 0, SCREEN_WIDTH,self.scrollerView.height);
    
    [self addChildViewController:self.vcTwo];
    [self.scrollerView addSubview:self.vcTwo.view];
    
    self.scrollerView.contentSize = CGSizeMake(SCREEN_WIDTH*2, 0);
    self.scrollerView.scrollEnabled = YES;
    self.scrollerView.delegate = self;
    self.scrollerView.showsHorizontalScrollIndicator = NO;
    self.lineView.frame = CGRectMake(SCREEN_WIDTH*0.5-30, 0, 60, 1);
    [APP_DELEGATE.window addSubview:self.alterView];
    
    
}
///点击目录
- (IBAction)clickClassName:(UIButton *)sender {
     self.lineView.frame = CGRectMake(SCREEN_WIDTH*0.25-30, 0, 60, 1);
    self.scrollerView.contentOffset = CGPointMake(0, 0);
    
}

///点击课程详情
- (IBAction)clickClassDet:(UIButton *)sender {
     self.lineView.frame = CGRectMake(SCREEN_WIDTH*0.25-30 +SCREEN_WIDTH*0.5, 0, 60, 1);
    self.scrollerView.contentOffset = CGPointMake(SCREEN_WIDTH, 0);
    
    
}
- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView{
    CGPoint poine = scrollView.contentOffset;
    if (poine.x == 0) {
        self.lineView.frame = CGRectMake(SCREEN_WIDTH*0.25-30, 0, 60, 1);
    }else{
        self.lineView.frame = CGRectMake(SCREEN_WIDTH*0.25-30 +SCREEN_WIDTH*0.5, 0, 60, 1);
    }

    
}
#pragma mark 点击事件
-(void)loadData{
    if ([UID integerValue] ==0) {
        [ZYXLoginViewController setupLogin];
        return;
    }
    
    [NetWork POSTWithUrlString:kInterfaceAllUrl(PATH_FenleiThree) parmas:@{@"zid":self.zid,@"user_id":UID} successBloock:^(NSDictionary *responseObject) {
        NSLog(@"responseObject =%@",responseObject);
        if ([responseObject[@"code"] isEqualToNumber:@200]) {
            
            ZYXClassModel *classModel  = [ZYXClassModel mj_objectWithKeyValues:responseObject];
            
            
            //is_vip是否会员 1:是  2：不是   is_buy是否购买 1：是 2：不是
            if ([classModel.is_vip isEqualToString:@"1"] || [classModel.is_buy isEqualToString:@"1"]) {
                self.isCanDown = YES;
            }else{
                self.isCanDown = NO;
            }
                
                 
           
        }else{
            [JKToast showWithText:responseObject[@"message"]];
        }
        
    } failureBlock:^(NSError *error) {
        
    }];
}
-(void)clicDown{
    if (self.isCanDown) {
        ZYXDownLoadVC *vc = [[ZYXDownLoadVC alloc]init];
        vc.zid = self.zid;
        [self.navigationController pushViewController:vc animated:YES];
    }else{
        self.alterView.hidden = NO;
    }
}
///立即购买
-(void)clickBuy{
    NSLog(@"00000");
    self.alterView.hidden = NO;
}
///关闭提示
-(void)clickCloseAlter{
   
    self.alterView.hidden = YES;
}
///开通权限
-(void)clickBuyAlter{
  
     self.alterView.hidden = YES;
    ZYXBuyVC *vc = [[ZYXBuyVC alloc]init];
    vc.typeStr = @"1";
    vc.zid = self.zid;
    [self.navigationController pushViewController:vc animated:YES];
}
///开通会员
-(void)clickVipAlter{
    
     self.alterView.hidden = YES;
    ZYXBuyVC *vc = [[ZYXBuyVC alloc]init];
    vc.typeStr = @"2";
    [self.navigationController pushViewController:vc animated:YES];
}
#pragma mark 协议
-(void)clickClassNameModel:(ZYXClassNameDerailModel *)model{
   
    
    if ([model.free isEqualToString:@"2"]) {
        [JKToast showWithText:@"该课程需要购买才能观看"];
        return;
    }
    
    [self.player _deallocPlayer];
    [self.freeBtn removeFromSuperview];
    [self.backImgview removeFromSuperview];
    
    if ([model.video_type isEqualToString:@"1"]) {
        self.typeStr = @"mp4";
    }else{
        self.typeStr = @"mp3";
    }
    self.configuration.sourceUrl = [NSURL URLWithString:model.video_url];
    _player = [[SelVideoPlayer alloc]initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH,250) configuration:self.configuration  withType:self.typeStr ];
    
    self.backImgview = [[UIImageView alloc]initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH,250)];
    ///图片地址
    [self.backImgview sd_setImageWithURL:[NSURL URLWithString:kImgAllUrl(model.free_img)] placeholderImage:IMG(@"mp3fengmian")];
    self.backImgview.userInteractionEnabled = YES;
    if ([self.typeStr isEqualToString:@"mp4"]) {
//            [self.backImgview addSubview:_player];
        [self.videoView addSubview:self.backImgview];
        [self.backImgview addSubview:_player];
      
    }else{
        [self.videoView addSubview:self.backImgview];
        [self.backImgview addSubview:_player];
    }
    self.freeBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    [self.freeBtn setImage:IMG(@"free-listen") forState:UIControlStateNormal];
    self.freeBtn.frame = CGRectMake(0, 0, 115, 35);
    [self.freeBtn addTarget:self action:@selector(clickPlay) forControlEvents:UIControlEventTouchUpInside];
    self.freeBtn.center = self.videoView.center;
     [self.videoView addSubview:self.freeBtn];
    if ([model.free isEqualToString:@"1"]) {
        self.freeBtn.hidden = YES;
    }
   



}
-(void)clickPlay{

        self.freeBtn.hidden = YES;
        [self.player _playVideo];
}
#pragma mark 懒加载
-(ZYXClassAlterView *)alterView{
    if (!_alterView) {
        _alterView = [[[NSBundle mainBundle]loadNibNamed:@"ZYXClassAlterView" owner:nil options:nil] lastObject];
        _alterView.frame = CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT);
        [_alterView.closeBtn addTarget:self action:@selector(clickCloseAlter) forControlEvents:UIControlEventTouchUpInside];
         [_alterView.buyJurisdictionBtn addTarget:self action:@selector(clickBuyAlter) forControlEvents:UIControlEventTouchUpInside];
         [_alterView.vipBtn addTarget:self action:@selector(clickVipAlter) forControlEvents:UIControlEventTouchUpInside];
        _alterView.hidden = YES;
    }
    
    return _alterView;
}

- (void)viewDidDisappear:(BOOL)animated
{
    [super viewDidDisappear:animated];
    [_player _deallocPlayer];
}

-(void)viewWillAppear:(BOOL)animated{
    
    
    
    [self.vcOne loadData];
    [self.vcTwo loadData];
}


@end
